Ext.define('DCApp.controller.ListController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.list',

	 getSelectedItem: function(){
		 return this.getView().getSelectionModel().getSelection()[0] || false;
	 },
/*	 onImport:function(){
		 Ext.onReady(function(){  
			    Ext.QuickTips.init();  
			    var win = new Ext.Window({  
			        title: '文件上传',  
			        //height: 400,  
			        // width: 300, 
			        draggable: false,  
			        items: [{
			  	        xtype: 'form',
			  	        //title: 'ExtJS 文件上传',
			  	        width: 400,
			  	        height: 160,
			  	        defaults: {
			  	          margin: '20 0 0 30', 
			  	        },
			  	        items: [{
			  	          xtype: 'filefield',
			 	          name: 'file',
			  	          fieldLabel: '上传文件',
			  	          buttonText: '选择文件',
			  	          labelWidth: 80,
			  	          width: 300,
			 	          allowBlank: false,
			  	          msgTarget: 'under',
			  	          validator: function(value){
			  	            var arr = value.split('.');
			  	            if(arr[arr.length-1] != 'xlsx'){
			  	              return '文件不合法！！！';
			 	            }else{
			  	              return true;
			  	            }
			  	          }
			  	        },{
			  	          xtype: 'button',
			  	          margin: '30 0 0 200',
			  	          text: '确定上传',
			  	          handler: function(b,e){
			  	            var form = b.up().getForm();
			  	            if(form.isValid()){
			  	              form.submit({
			  	                url:'uploadFile。do',
			  	                success: function(form, action){
			  	                  Ext.MessageBox.alert('提示:','上传成功');
			  	                },
			  	                failure:function(form, action){
			  	                  Ext.MessageBox.alert('提示:','上传失败');
			  	                }
			  	              });
			  	            }
			  	          }
			  	        }]
			  	      }]  
			    });  
			    win.show();  
		 }); 
			  	  
	 },*/
	onNew: function() {
     	var me=this;
		var obj = me.getView().getStore().getModel().create();
		me.fireViewEvent('openobj', me.getView(), obj,'ACTION_NEW');
/*    	obj.save({
    	success:function(rec) {
    		me.fireViewEvent('openobj', me.getView(), rec,'ACTION_NEW');
    	}});
*/    },
	onClone: function() {
		var vw = this.getView();
		var rec = vw.getSelectionModel().getSelection()[0] ;
		if(!rec){
			Ext.MessageBox.alert('操作提示', '请先选择待克隆的条目');
			return;
		}
		var rec2 = rec.copy(null);
    	rec2.save({
        	success:function(rec) {
        		 vw.getStore().load();
        }});
    },

    onEdit: function(view, rec) {
    	var vw = this.getView();
    	if(!vw.isXType('grid')){
    		vw = vw.down('dataview');
    	}
		var rec = vw.getSelectionModel().getSelection()[0] ;
		if(!rec){
			Ext.MessageBox.alert('操作提示', '请先选择待编辑的条目');
			return;
		}
		this.fireViewEvent('editobj',vw , rec);
	},
	//增强双击处理，对于处于detail中的list，默认为onEdit
	onDblClick: function(view, rec) {
		var vw = this.getView();
		var detailPanel = vw.up('detail');
		if(detailPanel){
			this.onEdit(view,rec);
		}else{
			this.fireViewEvent('openobj', this.getView(), rec,'ACTION_UPDATE');			
		}
	},

    onRefreshClick: function() {
        this.getView().getStore().load();
    },
    onRemove:function(){
    	var vw = this.getView();
    	//for dataview
    	if(!vw.isXType('grid')){
    		vw = vw.down('dataview');
    	}

    	var sm = vw.getSelectionModel();
		var sels = sm.getSelection();
		var store = vw.getStore();
		Ext.MessageBox.confirm('确认删除', '您确定要删除选中的[' + sels.length + ']项?',
			function(btn, text) {
				if (btn != 'yes')
					return false;
				//for simdata
				if(DCApp.Cfg.SIM_ENM){
					for(var i=0;i<sels.length;i++) {
						store.remove(sels[i]);
					}	
					return;
				}
				// 组织参数
				var relId = [];
				for(var i=0;i<sels.length;i++) {
					relId[i] = sels[i].get(sels[i].idProperty);
				}
				var relEntity = sels[0].entityName;
				Ext.Ajax.request({
		            url: '/rest/'+relEntity,
		            method: 'DELETE',
		            headers:{'Content-Type':'application/json;charset=UTF-8'},
		            jsonData :  {'ids':relId},
		            success: function(conn, response, options, eOpts) {
		            	vw.getSelectionModel().deselectAll();
					    Ext.toast({
				            title: '提示',
				            html: relId+'删除成功',
				            align: 't',
				            bodyPadding: 10
				        });
		            	vw.getStore().load();
		            },
		            failure: function(conn, response, options, eOpts) {

		            }
		        });

		});
    },
    onRelAdd:function(btn,e) {
    	var vw = this.getView();
		var sels = vw.getSelectionModel().getSelection();
		if (sels.length == 0) {
			Ext.MessageBox.alert('操作提示', '请先选择待关联的条目', null, this);
			return;
		}
		// 获取参数
		var eid = vw.ds.filters.items[0]._value;
		var property = vw.ds.filters.items[0]._property;
		var entity = property.substring(0, property.length-2).toUpperCase();
		var relId = [];
		for(var i=0;i<sels.length;i++) {
			var rec = sels[i];
			rec.set('ename',entity);
			rec.set('eid',eid);
		}
		var win = btn.up('window');
		vw.getStore().sync({
            success: function(conn, response, options, eOpts) {
        		vw.ds.load();
        		win.close();
            }			
		});
    },
    onRelDel:function() {
    	var vw = this.getView();
		var sels = vw.getSelectionModel().getSelection();
		if (sels.length == 0) {
			Ext.MessageBox.alert('操作提示', '请先选择待删除的条目', null, this);
			return;
		} else {
			Ext.MessageBox.confirm('确认删除', '您确定要删除选中的[' + sels.length + ']项?',
			function(btn, text) {
				if (btn != 'yes')
					return false;
				// 调用删除关联接口
				var id = vw.getStore().filters.items[0]._value;
				var property = vw.getStore().filters.items[0]._property;
				var entity = property.substring(0, property.length-2).toUpperCase();
				var relId = [];
				for(var i=0;i<sels.length;i++) {
					var rec = sels[i];
					rec.set('ename',null);
					rec.set('eid',0);
				}
				var win = btn.up('window');
				vw.getStore().sync({
		            success: function(conn, response, options, eOpts) {
		        		vw.ds.load();
		        		win.close();
		            }			
				});

			}, this);
		}
    },
    onRelList: function() {
    	var vw = this.getView();
    	var vtype;
    	//for dataview
    	if(!vw.isXType('grid')){
    		vw = vw.down('dataview');
    		vtype = vw.xtype.replace('view','list');
    	}else{
    		vtype = vw.xtype;
    	}
    	var ds = vw.getStore();

    	var vm = this.getViewModel();
    	var title = this.getView().up('[cls=nav_pos]').title;
    	var sel_win=vw.sel_win
    	if(!sel_win){
    		sel_win = new Ext.window.Window({
    			closeAction:'hide',
	        	width:document.body.clientWidth-150,
	        	height:document.body.clientHeight-150,
	        	layout:'fit',
	        	modal:true,
	        	//viewModel: vm,
	        	items:[{
	        			ds:ds,
	        			xtype:vtype,
	        			bind: {
	        		        store: '{objsel}'
	        		    },
	        			tbar: ['->',{
					        text: '增加关联',
					        handler: 'onRelAdd'
					    }]
	        		}
	        	],
	        	plain: true,
	            headerPosition: 'bottom'
	        });
	        vw.sel_win=sel_win;
    	}
    	sel_win.show();
    }

});
